package com.zjpavt.client.artnet.device;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zjpavt.client.artnet.ArtNetException;
import com.zjpavt.client.artnet.ArtNetServer;
import com.zjpavt.client.artnet.config.ConfigReader;
import com.zjpavt.client.artnet.effect.*;
import com.zjpavt.client.artnet.packets.ArtDmxPacket;
import com.zjpavt.client.enums.ExtendDeviceEnum;
import com.zjpavt.client.event.EventRegisterCenter;
import com.zjpavt.client.util.*;
import io.netty.util.concurrent.DefaultThreadFactory;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.SocketException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 512灯光弱电系统
 * 从配置文件中获取灯带信息，然后通过artNet发送指定信号
 * @author zyc
 */
@Slf4j
public class LightSystem {

    public static final String STRIP_LIGHT = "StripLight";
    private Map<String,StripLight> lightMap = new HashMap(8);
    private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1, new DefaultThreadFactory("Art-net"));
    private int period = 40;
    private ArtNetServer artNetServer;
    /**
     * 上一次按键 从0开始 小于12
     */
    private int lastPressKeyValue = 0;
    private static final LightSystem Instance = new LightSystem();
    public static LightSystem getInstance() {
        return Instance;
    }
    public LightSystem() {
        try {
            String lightSystem = PropertiesUtil.getPropertiesDefault("LightSystem", "false");
            if (!"true".equals(lightSystem)) {
                return;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        lightSystem();
        initServer();
        registerTaskByFile();

    }

    private void registerTaskByFile() {
        String configText = "";
        try {
            configText = FileUtil.readFromFile("/pavt/conf/effect.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Gson gson = new Gson();
        Type configReaderType = new TypeToken<List<ConfigReader>>() {} .getType();
        List<ConfigReader> configReaderList = new Gson().fromJson(configText, configReaderType);

        for (ConfigReader configReader : configReaderList) {
            String eventName = configReader.getEventName();
            Map<String,String> data = configReader.getParameter();
            String stripLightName = configReader.getStripLightName();
            IEffect effect = EffectFactory.getEffect(configReader.getEffectClass(), configReader.getEffectJson());
            IEffect nextEffect = EffectFactory.getEffect(configReader.getNextEffectClass(), configReader.getNextEffectJson());
            long outTime = configReader.getOutTime();
            if (eventName == null) {
                continue;
            }
            StripLight stripLight = lightMap.get(stripLightName);
            if (stripLight == null) {
                continue;
            } else {
                if (effect !=null) {
                    effect.setOwner(stripLight);
                }
                if (nextEffect != null) {
                    nextEffect.setOwner(stripLight);
                }
            }
            System.out.println(gson.toJson(configReader));
            switch (eventName) {
            case EventRegisterCenter.LIGHT_INIT:
                stripLight.changeTimeEffect(effect,outTime,nextEffect);
                break;
//              绿色正弦波流动 常态
            case EventRegisterCenter.DEVICE_DISCOVER:
//              常亮黄色
            case EventRegisterCenter.DEVICE_DISCONNECT:
                EventRegisterCenter.getInstance().registerTask(eventName,(map)-> {
                    String triggerDevcieName = (String)map.get(EventRegisterCenter.DEVICE_NAME);
                    int triggerExtNum = -1;
                    if (map.get(EventRegisterCenter.EXT_NUM) != null) {
                        triggerExtNum = (int)
                        map.get(EventRegisterCenter.EXT_NUM);
                    }
                    if (StringUtil.equals(triggerDevcieName,data.get(EventRegisterCenter.DEVICE_NAME) )) {
                        if (!data.containsKey(EventRegisterCenter.EXT_NUM)
                                || StringUtil.equals(data.get(EventRegisterCenter.EXT_NUM), String.valueOf((int)map.get(EventRegisterCenter.EXT_NUM)))) {
                            stripLight.changeTimeEffect(effect,outTime,nextEffect);
                        }
                    }
                });
                break;
            //              数据流动一秒后恢复原状
            case EventRegisterCenter.DATA_EXCHANGE:
                EventRegisterCenter.getInstance().registerTask(eventName,(map)-> {
                    String triggerDeviceName = (String)map.get(EventRegisterCenter.DEVICE_NAME);
                    int triggerExtNum = -1;
                    if (map.get(EventRegisterCenter.EXT_NUM) != null) {
                        triggerExtNum = (int)
                        map.get(EventRegisterCenter.EXT_NUM);
                    }
//                        如果设备名称相等
                    if (StringUtil.equals(triggerDeviceName,data.get(EventRegisterCenter.DEVICE_NAME) )) {
//                            如果注册时间没有拓展通道号码 或者 注册拓展通道的触发事件的拓展通道一致
                        if (!data.containsKey(EventRegisterCenter.EXT_NUM)
                                || StringUtil.equals(data.get(EventRegisterCenter.EXT_NUM), String.valueOf(triggerExtNum))) {
                            stripLight.changeTimeEffect(effect,outTime,nextEffect);
                        }
                    }
                });
                break;
//              无需参数判断 因为来自已知设备
            case EventRegisterCenter.DOOR_STATUS_CHANGE:
                EventRegisterCenter.getInstance().registerTask(eventName,(map)-> {
                    stripLight.changeTimeEffect(effect,outTime,stripLight.getEffect());
                });
                break;
//              时效指令处理完后切换状态
            case EventRegisterCenter.DOOR_STATUS_UPDATE:
                EventRegisterCenter.getInstance().registerTask(eventName,(map)-> {
                    IEffect ieffect;
                    if (EventRegisterCenter.OPEN.equals(map.get(EventRegisterCenter.STATUS))) {
                        ieffect = new SameColorEffect(stripLight, ColorConst.RED10);
                    } else {
                        ieffect = new SameColorEffect(stripLight, ColorConst.GREEN10);
                    }
                    stripLight.changeAfterTimeEffect(ieffect);
                });
                break;
//                    灯带数据流动 修改最后按键键值
            case EventRegisterCenter.KEY_PRESS:
                EventRegisterCenter.getInstance().registerTask(eventName,(map)-> {
                    int keyValue = (int)map.get(EventRegisterCenter.KEY_NAME);
                    if (keyValue>=0 && keyValue <3) {
                        this.lastPressKeyValue = keyValue;
                        stripLight.changeTimeEffect(effect,outTime,stripLight.getEffect());
                    }
                });
                break;
//                    根据最后按键 修改回路状态
            case EventRegisterCenter.LOOP_CHANGE:
                EventRegisterCenter.getInstance().registerTask(eventName,(map)-> {
                    String status = (String) map.get(EventRegisterCenter.LOOP_STATUS);
                    byte[] color;
                    if (Integer.parseInt(status.substring(lastPressKeyValue, lastPressKeyValue + 1)) % 2 == 0) {
                        color = ColorConst.RED10;
                    } else {
                        color = ColorConst.GREEN10;
                    }
                    stripLight.changeAfterTimeEffect(new SameColorEffect(stripLight, color));
                });
                break;
            default:
                break;
            }
        }

        EventRegisterCenter.getInstance().triggerRegisterTask(EventRegisterCenter.LIGHT_INIT,null);
//        Udp 监听事件 没有注册
        EventRegisterCenter.getInstance().registerTask(EventRegisterCenter.UDP_RECEIVED,(map)-> {
            if (map == null || !map.containsKey(EventRegisterCenter.MSG)) {
                return;
            } else {
                onUdpReceiveMessage((String)map.get(EventRegisterCenter.MSG));
            }
        });
    }
    /**
     * 注册事件
     */
    private void registerTask() {
//        按键触发事件
        EventRegisterCenter.getInstance().registerTask(EventRegisterCenter.KEY_PRESS,(map)-> {
            if (map == null || map.get(EventRegisterCenter.KEY_NAME) == null) {
                return;
            } else {
                onKeyPress((int)map.get(EventRegisterCenter.KEY_NAME));
            }
        });
        EventRegisterCenter.getInstance().registerTask(EventRegisterCenter.DEVICE_DISCOVER,(map)-> {
            if (map == null || map.get(EventRegisterCenter.DEVICE_NAME) == null) {
                return;
            } else {
                onDiscoverDevice((int)map.get(EventRegisterCenter.EXT_NUM),(String)map.get(EventRegisterCenter.DEVICE_NAME));
            }
        });
        EventRegisterCenter.getInstance().registerTask(EventRegisterCenter.DOOR_STATUS_UPDATE,(map)-> {
            if (map == null || map.get(EventRegisterCenter.STATUS) == null) {
                return;
            } else {
                onDoorStatusUpdate((String)map.get(EventRegisterCenter.STATUS));
            }
        });
        EventRegisterCenter.getInstance().registerTask(EventRegisterCenter.DOOR_STATUS_CHANGE,(map)-> {
            if (map == null || map.get(EventRegisterCenter.STATUS) == null) {
                return;
            } else {
                onDoorStatusChange((String)map.get(EventRegisterCenter.STATUS));
            }
        });


        EventRegisterCenter.getInstance().registerTask(EventRegisterCenter.DATA_EXCHANGE,(map)-> {
            String deviceName = (String)map.get(EventRegisterCenter.DEVICE_NAME);
            onDataExchange(deviceName);
        });
        EventRegisterCenter.getInstance().registerTask(EventRegisterCenter.LOOP_CHANGE,(map)-> {
            String status = (String) map.get(EventRegisterCenter.LOOP_STATUS);
            StripLight stripLight = lightMap.get(STRIP_LIGHT + 5);
            if (stripLight == null) {
                return;
            }
            byte[] color;
            if (Integer.parseInt(status.substring(lastPressKeyValue, lastPressKeyValue + 1)) % 2 == 0) {
                color = ColorConst.RED10;
            } else {
                color = ColorConst.GREEN10;
            }
            stripLight.changeAfterTimeEffect(new SameColorEffect(stripLight,color));
        });
        EventRegisterCenter.getInstance().registerTask(EventRegisterCenter.DEVICE_DISCONNECT,(map)-> {
            if (map == null || map.get(EventRegisterCenter.DEVICE_NAME) == null) {
                return;
            } else {
                onDisconnectDevice(0,(String)map.get(EventRegisterCenter.DEVICE_NAME));
            }
        });
        EventRegisterCenter.getInstance().registerTask(EventRegisterCenter.UDP_RECEIVED,(map)-> {
            if (map == null || !map.containsKey(EventRegisterCenter.MSG)) {
                return;
            } else {
                onUdpReceiveMessage((String)map.get(EventRegisterCenter.MSG));
            }
        });

//      摄像头特效
        StripLight stripLight = lightMap.get(STRIP_LIGHT + 2);
        if (stripLight == null) {
            return;
        }
        stripLight.setEffect(new SinWaveEffect(stripLight,ColorConst.GREEN10,30,0,-1));

    }

    /**
     * 数据交互
     * @param deviceName
     */
    private void onDataExchange(String deviceName) {
        ExtendDeviceEnum extendDeviceEnum = ExtendDeviceEnum.getDeviceByName(deviceName);
        StripLight stripLight;
        switch (extendDeviceEnum) {
        case GPS:
            stripLight = lightMap.get(STRIP_LIGHT + 1);
            if (stripLight == null) {
                return;
            }
            stripLight.setNextEffect(new SinWaveEffect(stripLight, ColorConst.GREEN10,30,0,-1));
            stripLight.setEffect(new SquareWaveEffect(stripLight, ColorConst.BLUE10,ColorConst.WHITE10,10,0));
            stripLight.setOutTime(1000);
            return;
        case TMP:
            stripLight = lightMap.get(STRIP_LIGHT + 8);
            if (stripLight == null) {
                return;
            }
            stripLight.setNextEffect(new SinWaveEffect(stripLight,ColorConst.GREEN10,stripLight.getLightNum()/2,0,-1));
            stripLight.setEffect(new SquareWaveEffect(stripLight, ColorConst.BLUE10,ColorConst.WHITE10,10,0));
            stripLight.setOutTime(1000);
            return;
        case ELECT:
            stripLight = lightMap.get(STRIP_LIGHT + 4);
            if (stripLight == null) {
                return;
            }
            stripLight.setEffect(new SquareWaveEffect(stripLight, ColorConst.BLUE10,ColorConst.WHITE10,10,0));
            stripLight.setNextEffect(new SinWaveEffect(stripLight,ColorConst.GREEN10,stripLight.getLightNum()/2,0,-1));
            stripLight.setOutTime(1000);
        default:
        }
    }

    /**
     * 启动周期Artnet发送
     */
    public void start() {
        if (scheduledThreadPoolExecutor.isShutdown()) {
            scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1, new DefaultThreadFactory("Art-net"));
        }
        scheduledThreadPoolExecutor.scheduleAtFixedRate(()-> {
            try {
                show();
            } catch (Exception e) {
                log.error(LoggerUtil.convertException2String(e));
            }
        }, 1000, period, TimeUnit.MILLISECONDS);
    }

    /**
     * 停止发送数据
     */
    public void stop() {
        scheduledThreadPoolExecutor.shutdownNow();
    }

    /**
     * 按键
     * @param keyValue
     */
    private void onKeyPress(int keyValue) {
        StripLight stripLight;
        switch (keyValue) {
        case 1:
        case 2:
        case 3:
            stripLight = lightMap.get(STRIP_LIGHT + 5);
            if (stripLight == null) {
                return;
            }
            this.lastPressKeyValue = keyValue -1;
            IEffect effect = new SquareWaveEffect(stripLight, ColorConst.BLUE10,ColorConst.WHITE10,15,0);
            stripLight.changeTimeEffect(effect,500,null);
            break;
        default:
        }
    }

    /**
     * 当发现设备的时候
     * @param extNum
     * @param deviceName
     */
    public void onDiscoverDevice(int extNum, String deviceName) {
        ExtendDeviceEnum extendDeviceEnum = ExtendDeviceEnum.getDeviceByName(deviceName);
        if (extendDeviceEnum == null) {
            return;
        }
        StripLight stripLight;
        switch (extendDeviceEnum) {
        case GPS:
            stripLight = lightMap.get(STRIP_LIGHT + 1);
            if (stripLight == null) {
                return;
            }
            stripLight.setEffect(new SinWaveEffect(stripLight,ColorConst.GREEN10,30,0,-1));
//                stripLight.setNextEffect(new SameColorEffect(stripLight,ColorConst.GREEN10));
//                stripLight.setOutTime(1000);
            break;
        case CIRCUIT:
            if (extNum == 0) {
                return;
            }
            stripLight = lightMap.get(STRIP_LIGHT + 3);
            if (stripLight == null) {
                return;
            }
            stripLight.setEffect(new SinWaveEffect(stripLight,ColorConst.GREEN10,6,0,-1));
            break;
        case ELECT:
            stripLight = lightMap.get(STRIP_LIGHT + 4);
            if (stripLight == null) {
                return;
            }
            stripLight.setEffect(new SinWaveEffect(stripLight,ColorConst.GREEN10,15,0,-1));
            break;
        case POWER:
            stripLight = lightMap.get(STRIP_LIGHT + 6);
            if (stripLight == null) {
                return;
            }
            stripLight.setEffect(new SquareWaveEffect(stripLight,ColorConst.WHITE10,ColorConst.BLUE10,10,0));
            stripLight.setNextEffect(new SameColorEffect(stripLight,ColorConst.GREEN10));
            stripLight.setOutTime(1000);
            break;
        case DOOR_ALARM:
            stripLight = lightMap.get(STRIP_LIGHT + 7);
            if (stripLight == null) {
                return;
            }
            stripLight.setEffect(new SquareWaveEffect(stripLight,ColorConst.WHITE10,ColorConst.BLUE10,10,0));
            stripLight.setNextEffect(new SameColorEffect(stripLight,ColorConst.RED10));
            stripLight.setOutTime(1000);
            break;
        case TMP:
            stripLight = lightMap.get(STRIP_LIGHT + 8);
            if (stripLight == null) {
                return;
            }
            stripLight.setEffect(new SquareWaveEffect(stripLight,ColorConst.WHITE10,ColorConst.BLUE10,10,0));
            stripLight.setNextEffect(new SameColorEffect(stripLight,ColorConst.GREEN10));
            stripLight.setOutTime(1000);
            break;

        default:
        }
    }

    /**
     * 当发现设备的时候
     * @param extNum
     * @param deviceName
     */
    public void onDisconnectDevice(int extNum, String deviceName) {
        ExtendDeviceEnum extendDeviceEnum = ExtendDeviceEnum.getDeviceByName(deviceName);
        if (extendDeviceEnum == null) {
            return;
        }
        StripLight stripLight;
        switch (extendDeviceEnum) {
        case GPS:
            stripLight = lightMap.get(STRIP_LIGHT + 1);
            if (stripLight == null) {
                return;
            }
            stripLight.changeEffect(new SameColorEffect(stripLight,ColorConst.YELLOW10));
            break;
        case CIRCUIT:
            stripLight = lightMap.get(STRIP_LIGHT + 3);
            if (stripLight == null) {
                return;
            }
            stripLight.changeEffect(new SameColorEffect(stripLight,ColorConst.YELLOW10));
            break;
        case ELECT:
            stripLight = lightMap.get(STRIP_LIGHT + 4);
            if (stripLight == null) {
                return;
            }
            stripLight.changeEffect(new SameColorEffect(stripLight,ColorConst.YELLOW10));
            break;
        case POWER:
            stripLight = lightMap.get(STRIP_LIGHT + 6);
            if (stripLight == null) {
                return;
            }
            stripLight.changeEffect(new SameColorEffect(stripLight,ColorConst.YELLOW10));
            break;
        case DOOR_ALARM:
            stripLight = lightMap.get(STRIP_LIGHT + 7);
            if (stripLight == null) {
                return;
            }
            stripLight.changeEffect(new SameColorEffect(stripLight,ColorConst.YELLOW10));
            break;
        case TMP:
            stripLight = lightMap.get(STRIP_LIGHT + 8);
            if (stripLight == null) {
                return;
            }
            stripLight.changeEffect(new SameColorEffect(stripLight,ColorConst.YELLOW10));
            break;

        default:
        }
    }

    /**
     * 当开关门报警的时候
     * @param doorStatus
     */
    public void onDoorStatusChange(String doorStatus) {
        StripLight stripLight = lightMap.get(STRIP_LIGHT + 7);
        if (stripLight == null) {
            return;
        }
        if ("close".equals(doorStatus)) {
            stripLight.setEffect(new SquareWaveEffect(stripLight, ColorConst.WHITE10, ColorConst.BLUE10, 10, 0));
            stripLight.setNextEffect(new SameColorEffect(stripLight, ColorConst.GREEN10));
            stripLight.setOutTime(1200);
        } else {
            stripLight.setEffect(new SquareWaveEffect(stripLight, ColorConst.WHITE10, ColorConst.BLUE10, 10, 0));
            stripLight.setNextEffect(new SameColorEffect(stripLight, ColorConst.RED10));
            stripLight.setOutTime(1200);
        }
    }

    /**
     * 当开关门报警的时候
     * @param doorStatus
     */
    public void onDoorStatusUpdate(String doorStatus) {
        StripLight stripLight = lightMap.get(STRIP_LIGHT + 7);
        if (stripLight == null) {
            return;
        }
        if ("close".equals(doorStatus)) {
            stripLight.changeAfterTimeEffect(new SameColorEffect(stripLight, ColorConst.GREEN10));
        } else {
            stripLight.changeAfterTimeEffect(new SameColorEffect(stripLight, ColorConst.RED10));
        }
    }

    /**
     * 相机
     * camera action=on|off
     *
     * 面板指示灯数据交互模式开启的外设列表。光感light 温湿度temp 抄表ammeter，可组合。
     * StateLightActionOn deviceId= zone=light|temp|ammeter
     *
     * 面板指示灯数据交互模式关闭的外设列表。光感light 温湿度temp 抄表ammeter，可组合。
     * StateLightActionOff deviceId= zone=light|temp|ammeter
     * 深圳对接陈博文手机端
     * @param msg
     */
    public void onUdpReceiveMessage(String msg) {
        String[] msgSplitByBlank = msg.split(" ");
        String eventKey = msgSplitByBlank[0];
        Map<String, String> dataMap = new HashMap<>();
        for (int i = 1; i < msgSplitByBlank.length; i++) {
            String[] dataSplitByEqual = msgSplitByBlank[i].split("=");
            if (dataSplitByEqual.length == 2) {
                dataMap.put(dataSplitByEqual[0], dataSplitByEqual[1]);
            }
        }
        String[] data;
        String deviceId;
        String zone;
        StripLight stripLight;
        String[] zoneList;

        switch (eventKey) {
        case "camera":
            stripLight = lightMap.get(STRIP_LIGHT +2);
            if (stripLight == null) {
                return;
            }
            String action = dataMap.get("action").trim();
            if ("on".equals(action)) {
                stripLight.changeTimeEffect(new SquareWaveEffect(stripLight, ColorConst.BLUE10, ColorConst.WHITE10, stripLight.getLightNum()/2, 0), Long.MAX_VALUE, stripLight.getEffect());
            } else if ("off".equals(action)) {
                if (stripLight.getNextEffect()!=null) {
                    stripLight.changeTimeEffect(stripLight.getNextEffect(),Long.MAX_VALUE,null);
                }
            }
            break;
        case "StateLightAction":
            deviceId = dataMap.get("deviceID");
            if (dataMap.containsKey("zoneOn")) {
                zone = dataMap.get("zoneOn");
                zoneList = zone.trim().split("\\|");
                for (String z : zoneList) {
                    stripLight = getStripByZone(z);
                    if (stripLight == null) {
                        continue;
                    }
                    SquareWaveEffect effect = new SquareWaveEffect(stripLight, ColorConst.BLUE10, ColorConst.WHITE10, stripLight.getLightNum() / 2, 0,-1);
                    if (stripLight.existActiveTimeEffect()) {
                        stripLight.changeTimeEffect(effect, Long.MAX_VALUE, stripLight.getNextEffect());
                    }
                    stripLight.changeTimeEffect(effect, Long.MAX_VALUE, stripLight.getEffect());
                }
            }
            if (dataMap.containsKey("zoneOff")) {
                deviceId = dataMap.get("deviceID");
                zone = dataMap.get("zoneOff");
                zoneList = zone.trim().split("\\|");
                for (String z : zoneList) {
                    stripLight = getStripByZone(z);
                    if (stripLight == null) {
                        continue;
                    }
                    if (stripLight.getNextEffect() != null) {
                        stripLight.changeTimeEffect(new SinWaveEffect(stripLight,ColorConst.GREEN10,stripLight.getLightNum()/2,0,-1), Long.MAX_VALUE, null);
                    }
                }
            }
            break;
        /*case "StateLightActionOn":
            deviceId = dataMap.get("deviceID");
            zone = dataMap.get("zone");
            zoneList = zone.split("\\|");
            for (String z : zoneList) {
                stripLight = getStripByZone(z);
                if (stripLight == null) {
                    continue;
                }
                IEffect effect = new SquareWaveEffect(stripLight, ColorConst.BLUE10, ColorConst.WHITE10, stripLight.getLightNum() / 2, 0);
                if (stripLight.existActiveTimeEffect()) {
                    stripLight.changeTimeEffect(effect, Long.MAX_VALUE, stripLight.getNextEffect());
                }
                stripLight.changeTimeEffect(effect, Long.MAX_VALUE, stripLight.getEffect());
            }
            break;
        case "StateLightActionOff":
            deviceId = dataMap.get("deviceID");
            zone = dataMap.get("zone");
           zoneList = zone.split("\\|");
            for (String z : zoneList) {
                stripLight = getStripByZone(z);
                if (stripLight == null) {
                    continue;
                }
                if (stripLight.getNextEffect() != null) {
                    stripLight.changeTimeEffect(stripLight.getNextEffect(), Long.MAX_VALUE, null);
                }
            }
                break;*/
        default:
        }

    }

    private StripLight getStripByZone(String zone) {
        StripLight stripLight;
        switch (zone) {
        case "light":
            stripLight = lightMap.get(STRIP_LIGHT + 1);
            break;
        case "temp":
            stripLight = lightMap.get(STRIP_LIGHT + 8);
            break;
        case "ammeter":
            stripLight = lightMap.get(STRIP_LIGHT + 6);
            break;
        case "projector":
            stripLight = lightMap.get(STRIP_LIGHT + 16);
            break;
        case "laser":
            stripLight = lightMap.get(STRIP_LIGHT + 18);
            break;
        case "washerlight":
            stripLight = lightMap.get(STRIP_LIGHT + 14);
            break;
        case "stagelamp":
            stripLight = lightMap.get(STRIP_LIGHT + 19);
            break;
        case "led":
            stripLight = lightMap.get(STRIP_LIGHT + 20);
            break;
        default:
            stripLight = null;
        }
        return stripLight;
    }

    /**
     * 初始化灯光系统 根据配置文件获取灯带信息
     * @return
     */
    private Map<String,StripLight> lightSystem() {
        for (int i = 1 ; i < 30; i++) {
            try {
                String lightName = STRIP_LIGHT + i;
                String lightConfig = PropertiesUtil.getProperties(ClientConfig.ART_NET_CONFIG_PROPERTIES, lightName,null);
                if (lightConfig != null) {
                    StripLight stripLight = new StripLight(lightConfig);
                    IEffect iEffect = new SameColorEffect(stripLight, ColorConst.BLACK);
                    stripLight.setEffect(iEffect);
                    stripLight.setDisplayName(lightName);
                    lightMap.put(lightName,stripLight);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        StripLight stripLight = lightMap.get(STRIP_LIGHT + 5);
        if (stripLight == null) {
            return lightMap;
        }
        stripLight.setEffect(new SameColorEffect(ColorConst.GREEN10));
        return lightMap;
    }

    /**
     * 初始化服务器
     * @return
     */
    private ArtNetServer initServer() {
        ArtNetServer artNetServer = new ArtNetServer(5441, 6454);
        try {
            String broadcaseAddress = PropertiesUtil.getProperties(ClientConfig.ART_NET_CONFIG_PROPERTIES,"broadcaseAddress",null);
            artNetServer.setBroadcastAddress(broadcaseAddress);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            artNetServer.start();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (ArtNetException e) {
            e.printStackTrace();
        }
        this.artNetServer = artNetServer;
        return artNetServer;
    }

    /**
     * 效果刷新 调用每一条的灯带的Effect
     */
    private void show() {
        Map<Integer, byte[]> dmxDataMap = new HashMap<>();
        for (StripLight s : lightMap.values()) {
            byte[] dmxData = dmxDataMap.getOrDefault(s.getUniverse(), new byte[512]);
            if (s.getEffect() != null) {
                s.getEffect().show();
            } else {
            }
            dmxData = s.append2DmxData(dmxData);
            dmxDataMap.put(s.getUniverse(), dmxData);
        }
        for (int universeIndex : dmxDataMap.keySet()) {
            byte[] data = dmxDataMap.get(universeIndex);
            ArtDmxPacket artDmxPacket = new ArtDmxPacket();
            artDmxPacket.setDMX(data, data.length);
            artDmxPacket.setUniverse(0, universeIndex - 1);
            artNetServer.broadcastPacket(artDmxPacket);
        }
    }
}
